<?php

namespace Futures\Controller;
use Futures\Model\OptionModel;
use Think\Controller;

/**
 *  坐市商模块
 */
class AgentOptionWriterController extends AgentBaseController{

	public function index(){
		$Model = new OptionModel('maker_log');			//实例化MongoDB
		$sql['maker_id'] = (int)session('AGENT.user_id');	//获取用户ID
		//资金类型判断
		 if($_GET['type'] == 1){
		 	$sql['money_type'] = array('in',array(1,4,5));
		 }elseif($_GET['type'] == 2){
		 	$sql['money_type'] = array('in',array(2,3));
		 }elseif($_GET['type'] == 3){
		 	$sql['money_type'] = array('in',array(6,7));
		 }
		/*
		 * 搜索功能
		 * */
		//昵称
		if(!empty($_GET['username'])){
			$map['nickname']=array('like',"%".trim($_GET['username'])."%");
			$user_id = M('Follow')->where($map)->field('id')->select();
			foreach($user_id as $k=>$v){
				$routes[$k] = $v['id'];
			}
			$sql['user_id'] = array('in',$routes);
		}
		//状态查询
		if($_GET['state'] && $_GET['state']!==-1){
            $sql['money_type'] = intval($_GET['state']);
			$status = $_GET['state'];
		}else{
			$status=-1;
		}
		if($_GET['option']){
			if($_GET['option']==1){
				$sql['order_id_type']=1;
				$option_id=1;
			}elseif($_GET['option']==2){
				$sql['order_id_type']=2;
				$option_id=2;
			}else{
				$option_id=-1;
			}
		}
        
		//创建时间查询
		if ($_GET['create_time']) {
			$time=strtotime($_GET['create_time']);
			$start_time=mktime(00,00,00,date('m',$time),date('d',$time),date('Y',$time));
			$end_time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
			$sql['create_time'] = array('between',array($start_time,$end_time));
			$create_time = $_GET['create_time'];
		}
		//操作时间查询
		if ($_GET['option_time']) {
			$time=strtotime($_GET['option_time']);
			$start_time=mktime(00,00,00,date('m',$time),date('d',$time),date('Y',$time));
			$end_time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
			$sql['option_time'] = array('between',array($start_time,$end_time));
			$option_time = $_GET['option_time'];
		}
		//分页
		$count = $Model->where($sql)->count();
		$Page = new \Think\Page($count,10);
		$show = $Page->show();// 分页显示输出
		$list = $Model->where($sql)->limit($Page->firstRow.','.$Page->listRows)->order('create_time desc')->select();				//查询该用户的资金明细
		
		//查询用户昵称,订单号
		foreach($list as $k => $v){
			$user = M('Follow')->where('id='.$v['user_id'])->field('nickname')->find();
			if($v['order_id_type'] == 1){
				$option = M('Futures_order')->where('id='.$v['option_id'])->field('transaction_id')->find();
			}elseif($v['order_id_type'] == 2){
				$Order = new OptionModel('order');
				$option = $Order->where('id='.$v['option_id'])->field('transaction_id')->find();
			}
			$list[$k]['user_name'] = $user['nickname'];
			$list[$k]['option'] = $option['transaction_id'];
		}
		//获取操作类型
		$money_log_type = C('MAKER_MONEY_LOG_TYPE');
		$this->assign('money_log_type',$money_log_type);
		$this->assign('create_time',$create_time);
		$this->assign('option_time',$option_time);
		$this->assign('option',$option_id);
		$this->assign('status',$status);
		$this->assign('_page',$show);
		$this->assign('list',$list);
		$this->display();
    }
    
    //导出数据
    public function dow(){
    	$Model = new OptionModel('maker_log');			//实例化MongoDB
    	$sql['maker_id'] = (int)session('AGENT.user_id');	//获取用户ID
    	//资金类型判断
    	if($_GET['type'] == 1){
    		$sql['money_type'] = array('in',array(1,4,5));
    	}elseif($_GET['type'] == 2){
    		$sql['money_type'] = array('in',array(2,3));
    	}elseif($_GET['type'] == 3){
    		$sql['money_type'] = array('in',array(6,7));
    	}
    	/*
    	 * 搜索功能
    	* */
    	//昵称
    	if(!empty($_GET['username'])){
    		$map['nickname']=array('like',"%".trim($_GET['username'])."%");
    		$user_id = M('Follow')->where($map)->field('id')->select();
    		foreach($user_id as $k=>$v){
    			$routes[$k] = $v['id'];
    		}
    		$sql['user_id'] = array('in',$routes);
    	}
    	//状态查询
		if($_GET['state'] && $_GET['state']!==-1){
            $sql['money_type'] = intval($_GET['state']);
			$status = $_GET['state'];
		}else{
			$status=-1;
		}
   		if($_GET['option']){
			if($_GET['option']==1){
				$sql['order_id_type']=1;
				$option_id=1;
			}elseif($_GET['option']==2){
				$sql['order_id_type']=2;
				$option_id=2;
			}else{
				$option_id=-1;
			}
		}
    	//创建时间查询
    	if ($_GET['create_time']) {
    		$time=strtotime($_GET['create_time']);
    		$start_time=mktime(00,00,00,date('m',$time),date('d',$time),date('Y',$time));
    		$end_time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
    		$sql['create_time'] = array('between',array($start_time,$end_time));
    		$create_time = $_GET['create_time'];
    	}
    	//操作时间查询
    	if ($_GET['option_time']) {
    		$time=strtotime($_GET['option_time']);
    		$start_time=mktime(00,00,00,date('m',$time),date('d',$time),date('Y',$time));
    		$end_time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
    		$sql['option_time'] = array('between',array($start_time,$end_time));
    		$option_time = $_GET['option_time'];
    	}
    	
    	$list = $Model->where($sql)
    	->field('user_id,money,money_type,option_id,order_id_type,option_time,create_time')
    	->select();				//查询该用户的资金明细
    	//获取坐市商名称
    	$user_id = session('AGENT.user_id');
    	$user = M('follow')->where('id='.$user_id)->field('nickname')->find();
    	//设置文件名称
    	$filename= $user['nickname'].'的资金明细.csv';
		//设置排头
		header('Content-Type: application/vnd.ms-excel.numberformat ');
		header('Content-Disposition: attachment;filename='.$filename.'');
		header('Cache-Control: max-age=0');
		//要导出的数据设置
    	$data .= "用户昵称,";
		$data .= "操作资金,";
		$data .= "操作类型,";
		$data .= "订单号,";
		$data .= "订单类型,";
		$data .= "操作时间,";
		$data .= "创建时间,\n";
   		//修改字段
		foreach($list as $k => $v){
			//修改用户昵称
			$user = M('Follow')->where('id='.$v['user_id'])->field('nickname')->find();
			$list[$k]['user_id'] = $user['nickname'];
			//修改订单号
			$option = M('Futures_order')->where('id='.$v['option_id'])->field('transaction_id')->find();
			$list[$k]['option_id'] = "Oder:".$option['transaction_id'];
			//修改操作类型
			if($v['money_type'] == 1){
				$list[$k]['money_type'] = "下单";
			}elseif($v['money_type'] == 2){
				$list[$k]['money_type'] = "充值";
			}elseif($v['money_type'] == 3){
				$list[$k]['money_type'] = "提现";
			}elseif($v['money_type'] == 4){
				$list[$k]['money_type'] = "佣金";
			}elseif($v['money_type'] == 5){
				$list[$k]['money_type'] = "盈利";
			}elseif($v['money_type'] == 6){
				$list[$k]['money_type'] = "充值手续费";
			}elseif($v['money_type'] == 7){
				$list[$k]['money_type'] = "提现手续费";
			}
			//修改时间格式
			$list[$k]['option_time'] = date("Y/m/d",$v['option_time']);
			$list[$k]['create_time'] = date("Y/m/d",$v['create_time']);
			//修改订单号类型
			if($v['order_id_type'] == 1){
				$list[$k]['order_id_type'] = "螺纹钢类型";
			}elseif($v['order_id_type'] == 2){
				$list[$k]['order_id_type'] = "恒指类型";
			}
			
		}
		//编辑需导出的数据
		foreach($list as $v){
			foreach($v as $key => $value){
				$va = str_replace(',','，',$value);
				if($key == "create_time"){
					$vo = $va.",\n";
				}else{
					$vo = $va.",";
				}
				if($key != "_id"){
					$data .= $vo;
				}
			}
			
		}
		echo  @mb_convert_encoding($data, 'GB2312','utf-8');
    }
}
